package com.xy.redisson;

import com.google.common.base.Preconditions;
import com.xy.redisson.constant.RedisConnectionType;
import com.xy.redisson.properties.RedissonProperties;
import com.xy.redisson.service.RedissonConfigService;
import com.xy.redisson.service.impl.ClusterConfigImpl;
import com.xy.redisson.service.impl.MasterSlaveConfigImpl;
import com.xy.redisson.service.impl.SentineConfigImpl;
import com.xy.redisson.service.impl.StandaloneConfigImpl;
import lombok.extern.slf4j.Slf4j;
import org.redisson.Redisson;
import org.redisson.config.Config;

/**
 * @author xxy
 * @date 2022/1/5 20:55
 */
@Slf4j
public class RedissonManager {


    private Config config = new Config();
    private Redisson redisson = null;

    public RedissonManager() {
    }

    public RedissonManager(RedissonProperties redissonProperties) {

        try {
            //通过不同部署方式获得不同config实体
            config = RedissonConfigFactory.getInstance().createConfig(redissonProperties);
            redisson = (Redisson) Redisson.create();
        } catch (Exception e) {
            log.error("Redisson init error", e);
            throw new IllegalArgumentException("please input correct configurations," +
                    "connectionType must in standalone/sentinel/cluster/masterslave");
        }

    }

    public Redisson getRedisson() {
        return redisson;
    }

    static class RedissonConfigFactory {
        private RedissonConfigFactory() {
        }

        private static class Inner {
            static RedissonConfigFactory factory = new RedissonConfigFactory();
        }

        public static RedissonConfigFactory getInstance() {
            return Inner.factory;
        }

        Config createConfig(RedissonProperties redissonProperties) {
            Preconditions.checkNotNull(redissonProperties);
            Preconditions.checkNotNull(redissonProperties.getAddress(), "redisson.lock.server.address cannot be NULL!");
            Preconditions.checkNotNull(redissonProperties.getType(), "redisson.lock.server.password cannot be NULL");
            Preconditions.checkNotNull(redissonProperties.getDatabase(), "redisson.lock.server.database cannot be NULL");

            String connectionType = redissonProperties.getType();

            //声明配置上下文
            RedissonConfigService redissonConfigService = null;

            if (connectionType.equals(RedisConnectionType.STANDALONE.getConnection_type())) {
                redissonConfigService = new StandaloneConfigImpl();
            } else if (connectionType.equals(RedisConnectionType.SENTINEL.getConnection_type())) {
                redissonConfigService = new SentineConfigImpl();
            } else if (connectionType.equals(RedisConnectionType.CLUSTER.getConnection_type())) {
                redissonConfigService = new ClusterConfigImpl();
            } else if (connectionType.equals(RedisConnectionType.MASTERSLAVE.getConnection_type())) {
                redissonConfigService = new MasterSlaveConfigImpl();
            } else {
                throw new IllegalArgumentException("创建Redisson连接Config失败！当前连接方式:" + connectionType);
            }
            return redissonConfigService.createRedissonConfig(redissonProperties);
        }
    }

    /*static class RedissonConfigFactory{
        private RedissonConfigFactory() {
        }
        private static volatile RedissonConfigFactory factory = null;

        public static RedissonConfigFactory getInstance(){
            if (factory == null){
                synchronized (RedissonConfigFactory.class){
                    if (factory == null){
                        factory = new RedissonConfigFactory();
                    }
                }
            }
            return factory;
        }
    }*/


}
